#pragma once
#include<iostream>
#include<pthread.h>

namespace LockModule
{
    class Mutex
    {
        Mutex(const Mutex& mtx) = delete;
        const Mutex& operator = (const Mutex&mtx) = delete;

        public:
        Mutex()
        {
            int n = pthread_mutex_init(&_lock,nullptr);
            (void)n;
        }
        ~Mutex()
        {
            int n = pthread_mutex_destroy(&_lock);
            (void)n;
        }

        void Lock()
        {
            int n = pthread_mutex_lock(&_lock);
            (void)n;
        }
        void Unlock()
        {
            int n = pthread_mutex_unlock(&_lock);
            (void)n;
        }

        pthread_mutex_t* addMutex()
        {
            return &_lock;
        }

        private:
        pthread_mutex_t _lock;
    };

    class Lockguard
    {
        public:
        Lockguard(Mutex& mtx):_mtx(mtx)
        {
            _mtx.Lock();
        }

        ~Lockguard()
        {
            _mtx.Unlock();
        }

        private:
        Mutex& _mtx;

    };
}